/*-------------------------------------------------------------------------

***************** Modification History ***********************************
   @doc
   @module merch.c - Multiple Merchant functions |
   @end
   
$Log: /SoftPay/SPK2SIM/Source/Merch/merch.c $ 
**************************************************************************

   Copyright (C) 1999 by VeriFone, Inc.

   All rights reserved.  No part of this software may be reproduced,
   transmitted, transcribed, stored in a retrieval system, or translated
   into any language or computer language, in any form or by any means,
   electronic, mechanical, magnetic, optical, chemical, manual or otherwise,
   without the prior written permission of VeriFone, Inc.

---------------------------------------------------------------------------*/
#include <project.h>
#define MERCH_C

#include <stdlib.h>
#include <applidl.h>
#include <aclconio.h>
#include <dbmgr.h>
#include <formater.h>
#include <string.h>


/** /#define DEBUG_MERCH*/
#ifdef DEBUG_MERCH
#define DEBUG_95
#endif

#include <define.h>
#include <transact.h>
#include <merch.h>
#include <batmsg.h>
#include <mit.h>
#include <tblutil.h>
#include <tct.h>
#include <confio.h>
#include <msg.h>
#include <entry.h>
#include <ui.h>
#include <menufunc.h>
//#include <menus.h>
#include <msgfuncs.h>
#include <passwd.h>
#include <spdebug.h>
#include <codesec.h>


#define     SELECT_MERCH_NONE   0
#define     SELECT_MERCH_ONE    1
#define     SELECT_MERCH_ALL    inGetMITTotRecs()

/********************************************************************************************* 
* @func int | inGetCheckMID | 
*       Prompts user to select a merchant and confirms the merchant is valid. 
*
* @parm TRANSACTION_OBJECT *    | pobTran | 
*
* @rdesc Return values.
* @flag int    |  VS_ERR, VS_ESCAPE or VS_SUCCESS
* @end
**********************************************************************************************/

int inGetCheckMID(TRANSACTION_OBJECT* pobTran) {
    int inCnt,
    inNumOfMerch,
    inMerchNum;
    int inKey,
    inRetVal,
    inNumChars;
    int inOptCreated = 0;
    char szDisplayBuffer[MESSAGE_SIZE + 1];
    char szUserInput[MERCH_INDEX_SIZE + 1];
    char szLineBuf[4][MESSAGE_SIZE + 1];

    int inKeyPrev = KEY_e;
    int inKeyNext = KEY_f;
    int inKeyExit = KEY_h;
    int inKeySel = KEY_g;   

    /** / pdebug(("--inGetCheckMID--")); */

    memset(szDisplayBuffer, '\0', sizeof(szDisplayBuffer));
    memset(szUserInput, '\0', sizeof(szUserInput));
    inCnt = 0;
    inMerchNum = 0;
    inNumChars = 0;
    inNumOfMerch = inGetMITTotRecs();

    /* load and display merchant, get user merchant selection */
    /* only load, display, and get response if more than one merchant */
    if (inNumOfMerch > 1)
        do {
            if (inLoadMITRec(inCnt) != VS_SUCCESS)
                return(VS_ERR);

            sprintf(szDisplayBuffer, SUBSTITUTE_D, inGetMerchantNum());
            strcat(szDisplayBuffer, "=");

            vdDisplayAt(obDisp.inHomeCol, obDisp.inTablesInputLine, szDisplayBuffer, CLR_EOL);
            memset(szDisplayBuffer, '\0', sizeof(szDisplayBuffer));
            vdDisplayAt(obDisp.inHomeCol, obDisp.inTablesInputLine + 4, szDisplayBuffer, CLR_EOL);
            vdDisplayAt(obDisp.inHomeCol, obDisp.inTablesInputLine + 2, szDisplayBuffer, CLR_EOL);
            strlcpy(szDisplayBuffer, szGetMerchantText(), MESSAGE_SIZE);
            vdDisplayAt(obDisp.inHomeCol, obDisp.inTablesInputLine + 1, szDisplayBuffer, CLR_EOL);

            /* 3200: parse keys, display in right window */
            vdLoad3200Opt(szLineBuf, O3200_TBL_CHOOSE_KEYS_MSG);
            vdCreateOptWin3200(szLineBuf);  
            inOptCreated = 1;

            do {
                /* get first character */
                inKey = inWaitKey(inGetIdleTimeOut());
                inNumChars = 1;

                /* if numeric key press, get second digit or ENTER */
                if ((inKey > KEY0) && (inKey <= KEY9)) {
                    /* store and display first character */
                    szUserInput[0] = inKey;
                    vdDisplayAt(obDisp.inHomeCol, obDisp.inDetailLine + 4, szUserInput, CLR_EOL);

                    do {
                        /* get second character */
                        inKey = inWaitKey(inGetIdleTimeOut());
                        inNumChars = 2;

                        switch (inKey) {
                        case KEY0:
                        case KEY1:
                        case KEY2:
                        case KEY3:
                        case KEY4:
                        case KEY5:
                        case KEY6:
                        case KEY7:
                        case KEY8:
                        case KEY9:
                            /* store and display second character */
                            szUserInput[1] = inKey;
                            vdDisplayAt(obDisp.inHomeCol, obDisp.inDetailLine + 4, szUserInput, CLR_EOL);

                            do {
                                /* get third character */
                                inKey = inWaitKey(inGetIdleTimeOut());

                                while ((inKey != KEY_CR) && (inKey != KEY_CLR) && (inKey != KEY_BS) && (inKey != KEY_CANCEL)) {
                                    vdBeep();
                                    /* wait for another key */
                                    inKey = inWaitKey(inGetIdleTimeOut());
                                }

                                switch (inKey) {
                                case KEY_CR:
                                    /* confirm merchant number is valid */
                                    if ((atoi(szUserInput) > 0) && (atoi(szUserInput) <= inNumOfMerch)) {
                                        inMerchNum = atoi(szUserInput);
                                        inRetVal = VS_SUCCESS;
                                    } else {
                                        /* display "Invalid Merchant" ?? */
                                        memset(szUserInput, '\0', sizeof(szUserInput));
                                        vdDisplayAt(obDisp.inHomeCol, obDisp.inDetailLine + 4, szUserInput, CLR_EOL);
                                        inNumChars = 0;
                                        vdBeep();
                                    }
                                    break;
                                case KEY_BS:
                                    szUserInput[1] = '\0';
                                    inNumChars = 1;
                                    vdDisplayAt(obDisp.inHomeCol, obDisp.inDetailLine + 4, szUserInput, CLR_EOL);
                                    break;
                                case KEY_CLR:
                                case KEY_CANCEL:
                                    inMerchNum = VS_ESCAPE;
                                    inRetVal = VS_ESCAPE;
                                    break;
                                default:
                                    vdBeep();
                                }
                                if ((inNumChars == 1) || (inNumChars == 0))
                                    break;
                            } while (inMerchNum == 0);
                            break;
                        case KEY_CR:
                            /* confirm merchant number if valid */
                            if ((atoi(szUserInput) > 0) && (atoi(szUserInput) <= inNumOfMerch)) {
                                inMerchNum = atoi(szUserInput);
                                inRetVal = VS_SUCCESS;
                            } else {
                                /* display "Invalid Merchant" ?? */
                                memset(szUserInput, '\0', sizeof(szUserInput));
                                vdDisplayAt(obDisp.inHomeCol, obDisp.inDetailLine + 4, szUserInput, CLR_EOL);
                                inNumChars = 0;
                                vdBeep();
                            }
                            break;
                        case KEY_CANCEL:
                            inMerchNum = VS_ESCAPE;
                            inRetVal = VS_ESCAPE;
                            break;
                        case KEY_BS:
                            szUserInput[0] = '\0';
                            inNumChars = 0;
                            vdDisplayAt(obDisp.inHomeCol, obDisp.inDetailLine + 4, szUserInput, CLR_EOL);
                            break;
                        default:
                            vdBeep();
                        } /* switch in second char */
                        if (inNumChars == 0)
                            break;
                    } while (inMerchNum == 0);
                } else if (inKey == inKeyPrev) {
                    if (inCnt == 0)
                        inCnt = inNumOfMerch - 1;
                    else
                        inCnt = inCnt - 1;
                    inMerchNum = 0;
                    break;
                } else if (inKey == inKeyNext) {
                    if (inCnt == (inNumOfMerch - 1))
                        inCnt = 0;
                    else
                        inCnt = inCnt + 1;
                    inMerchNum = 0;
                    break;
                } else if ((inKey == inKeySel) || (inKey == KEY_CR)) {
                    inMerchNum = inCnt + 1;
                    inRetVal = VS_SUCCESS;
                } else if ((inKey == inKeyExit) || (inKey == KEY_CANCEL)) {
                    inMerchNum = VS_ESCAPE;
                    inRetVal = VS_ESCAPE;
                } else
                    vdBeep();
            } while (inMerchNum == 0);
        } while (inMerchNum == 0);
    else {
        inMerchNum = 1;
        inRetVal = VS_SUCCESS;
    }

    /* clear display */
    vdClearNonTitleLines();
    if (inOptCreated)
        vdDestroyOptWin3200(); 

    if (inMerchNum != VS_ESCAPE) {
        if (inLoadMITRec(inMerchNum - 1) != VS_SUCCESS)
            return (VS_ERR);
    } else
        return (VS_ESCAPE);

    if (fGetMerchPWFlag())
        inRetVal = inGetMerchPassword(pobTran);

    return (inRetVal);
}

/********************************************************************************************* 
* @func int | inGetMerchPassword | 
*       Prompts user for the merchant password.  If incorrect merchant password is
*       entered, reprompt password.  If CLEAR is pressed at password prompt, return
*       to idle. 
*
* @parm TRANSACTION_OBJECT *    | pobTran | 
*
* @rdesc Return values.
* @flag int    |  VS_ERR, VS_ESCAPE or VS_SUCCESS
* @end
**********************************************************************************************/

int inGetMerchPassword(TRANSACTION_OBJECT* pobTran) {
    int inResult;
    char szPassword[PASSWORD_SIZE + 1];
    char szMerchPassword[PASSWORD_SIZE + 1];

    while (1) {
        if ((inResult = inGetPassword(MERCH_PW_MSG15, szPassword)) <= VS_ERR) {
            inResult = VS_ESCAPE;
            break;
        } else {
            strcpy(szMerchPassword, szGetMerchPasswd());

            if (strcmp(szMerchPassword, szPassword) != 0)
                vdDisplayMessageFromFile(INV_PWD_ERR_MSG21, WARNING_MSG);
            else {
                inResult = VS_SUCCESS;
                break;
            }
        }
    }       

    vdClearNonTitleLines();
    return (inResult);
}

/********************************************************************************************* 
* @func int | inSelectOneAllMID | 
*       Prompts user to select ONE merchant or ALL merchants.  If ONE is selected,
*       the user is prompted to select which valid merchant. 
*
* @parm TRANSACTION_OBJECT *    | pobTran | 
*
* @rdesc Return values.
* @flag int    |  SELECT_MERCH_NONE - if canceled by user |
* @flag int    |  SELECT_MERCH_ALL |
* @flag int    |  SELECT_MERCH_ONE - if only one merchant in MIT |
* @flag int    |  User selected merchant number index   |
* @end
**********************************************************************************************/

int inSelectOneAllMID(TRANSACTION_OBJECT* pobTran,VS_BOOL fPromptMerchFlag) {
    signed int inResult;
    int inNumOfMerch;
    int inSettleMerchant;
    char* chNull = 0;

    /** / pdebug(("--inSelectOneAllMID--")); */
    inNumOfMerch = inGetMITTotRecs();
    inResult = 0;

    /* NOTE: THIS FUNCTION DOES NOT CHECK FOR AUTO SETTLE
        CHECK FOR AUTO SETTLE BEFORE CALL TO THIS FUNCTION */

    if (!fPromptMerchFlag)
        inSettleMerchant = SELECT_MERCH_ALL;
    else if ((inNumOfMerch > 1) && (fPromptMerchFlag)) {
        if ((inResult = obDisp.inDO_ChooseOne(chNull, pchGetMsg(EDIT_HFT_ITEM_MSG15), pchGetMsg(ALL_ONE_MSG5x), 2, NULL_PFI)) != VS_ESCAPE) {
            if (inResult == 1) {
                /* ALL merchants selected */
                inSettleMerchant = SELECT_MERCH_ALL;
            } else {
                /* ONE merchant selected - get which one */
                if ((inResult = inGetCheckMID(pobTran)) == VS_SUCCESS)
                    inSettleMerchant = SELECT_MERCH_ONE;
                else
                    inSettleMerchant = SELECT_MERCH_NONE;
            }
        } else
            inSettleMerchant = SELECT_MERCH_NONE;
    } else
        inSettleMerchant = SELECT_MERCH_ONE;

    return (inSettleMerchant);
}

int MERCH_CODE_CHECK(char* a,char* b,short* c,char* d,unsigned char* e) {
    GET_VER(a, b, c, d, e);
    return 1;
}



#undef MERCH_C
